import router from "./router/index";
import store from "./store/index";
import {
	Message
} from "element-ui";

// progress bar
import NProgress from "nprogress";
import "nprogress/nprogress.css";

const whiteList = ["/login", "/revisePwd", "/admin/Login/index"];
NProgress.configure({
	showSpinner: false
});

router.beforeEach((to, from, next) => {
	let access = JSON.parse(localStorage.getItem("access"));
	let token = localStorage.getItem("loginToken") || "";
	//let token = "sdfsdf";
	let accessArr = [];
	NProgress.start();
	accessArr = access ? access : [];
	if (token) {
		if (to.path == "/login") {
			next({
				path: "/home"
			});
			NProgress.done();
		} else {
			if (to.meta.title) {
				localStorage.setItem('componentTitle', to.meta.title);
				if (localStorage.getItem('componentTitle').indexOf('机终端') > -1) {
					store.commit("Set_componentTitle", to.meta.title);
				}
				// if (store.getters.access.length === 0 && access) {
				// 	store.commit("set_loginstate", accessArr);
				// 	store.dispatch("GenerateRoutes", accessArr)
				// 		.then(() => {
				// 			router.addRoutes(store.getters.addRouters);
				// 			next({
				// 				...to,
				// 				replace: true
				// 			});
				// 		})
				// 		.catch(err => {});
				// } else {
				// 	next();
				// }
				
			}
			next();
		}
	} else {
		if (whiteList.includes(to.path)) {
			next();
		} else {
			next("/login");
			NProgress.done();
		}
	}
});

router.afterEach(() => {
	NProgress.done();
});
